package com.superyee.commonlib.utils;

import android.app.Activity;

import java.util.Iterator;
import java.util.Stack;

public class ATStackUtil {


    private Stack<Activity> activityStack = new Stack<Activity>();

    private ATStackUtil() {

    }

    private static class SingletonInstance {
        private static final ATStackUtil INSTANCE = new ATStackUtil();
    }

    public static ATStackUtil getInstance() {
        return SingletonInstance.INSTANCE;
    }


    //关闭应用不会清空登录信息
    public void closeApp() {
        // 置空静态变量
        while (!activityStack.isEmpty()) {
            Activity at = activityStack.lastElement();
            if (at != null) {
                at.finish();
            }
            popActivity(at);
        }
    }

    public void finishActivity(String acName) {
        Iterator iterator = activityStack.iterator();
        while (iterator.hasNext()) {
            Activity activity = (Activity) iterator.next();
            if (activity.getClass().getName().equals(acName)) {
                activity.finish();
                activityStack.remove(activity);
                break;
            }
        }
    }

    public void pushActivity(Activity activity) {
        activityStack.add(activity);
    }


    /**
     * activity出栈
     *
     * @param activity
     */
    public void popActivity(Activity activity) {
        if (null != activity) {
            activityStack.remove(activity);
            activity = null;
        }
    }

     public Activity currentActivity(){
         Activity activity=null;
         try {
             if(activityStack!=null&&activityStack.size()!=0){
            activity  = activityStack.lastElement();
            }
         } catch (Exception e) {
             e.printStackTrace();
         }
         return activity;
     }

}
